package com.datang.pet.api.petController;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-21 18:39
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.redis.RedisTool;
import com.datang.pet.common.response.Result;
import com.datang.pet.common.utils.MD5;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.pojo.Pet;
import com.datang.pet.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AddPetController {

    @Autowired
    private PetService petService;

    @Autowired
    private RedisTool redisTool;

    @PostMapping("/addPet")
    public Result addPet(@RequestBody Pet pet) throws Exception {

        if (!StringUtils.isNUll(pet.getBelongUserId())) {
            throw new Exception("用户id为空");
        }
        if (!StringUtils.isNUll(pet.getName())) {
            throw new Exception("name为空");
        }
        if (!StringUtils.isNUll(pet.getGender())) {
            throw new Exception("性别为空");
        }
        if (!StringUtils.isNUll(pet.getBreed())) {
            throw new Exception("品种为空");
        }
        if (pet.getBirthday()==null) {
            throw new Exception("生日为空");
        }

        if (!StringUtils.isNUll(pet.getPictrue())) {
            throw new Exception("照片为空");
        }
        try {
            pet.setBelongUserId(MD5.decryptData(pet.getBelongUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }



        petService.addPet(pet);

        redisTool.remove("getAllPetByUserId"+pet.getBelongUserId());

        return Result.ok("ok");
    }
}
